package pm.gui.components;

import javax.swing.*;
import java.awt.*;

/**
 * @author Anton Bernatski
 * @version $Revision$ $Date$
 */
public class ImagePanel extends JPanel {
	public enum BackgroundMode {
		CENTER,
		STRETCH,
		FILL
	}

	private Image image;
	private BackgroundMode backgroundMode;

	public ImagePanel(final Image image, BackgroundMode backgroundMode) {
		this.image = image;
		this.backgroundMode = backgroundMode;
	}

	@Override
	protected void paintComponent(final Graphics g) {
		final int imageHeight = image.getHeight(null);
		final int imageWidth = image.getWidth(null);
		switch (backgroundMode) {
			case CENTER: {
				final int x = (getWidth() - imageWidth) / 2;
				final int y = (getHeight() - imageHeight) / 2;
				g.drawImage(image, x, y, null);
				break;
			}
			case FILL: {
				for (int x = 0; x < getWidth(); x += imageWidth) {
					for (int y = 0; y < getHeight(); y += imageHeight) {
						g.drawImage(image, x, y, null);
					}
				}
				break;
			}
			case STRETCH: {
				g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
				break;
			}
		}
	}
}
